import {toPlainObjectDeep} from '../../../utils/toPlainObjectDeep';
import {observable, action} from 'mobx';
import {TagBean, TagSearchBean, TagController} from '../../../model/tag/Tag';
import {AdAppPositionController} from '../../../model/ad/adPosition/AdAppPositionController'
import _ from 'lodash';
import {FormStore, ListStore, FieldItem, isRequired, isSelectRequired, toJSDeep} from '../../common/store/Store';
import {message} from 'antd';
import {isNotHan} from "../../../utils/validator/Validator";

const tagController = new TagController();

export class TagFields implements TagBean<FieldItem> {
    constructor(type = 'add') {
        if (type === 'edit') {
            _.forEach(this, (field: FieldItem, key: string) => {
                if (field.hasFeedback === true) {
                    field.validateStatus = 'success';
                }
            })
        }
    }

    id: FieldItem = new FieldItem({
        value: null,
    });

    appId: FieldItem = new FieldItem({
        value: -1,
        hasFeedback: true,
        required: true,
        disabled: false,
        option: [{
            id: -1,
            name: '请选择',
            title: '请选择',
            description: '请选择'
        }]
    });

    code: FieldItem = new FieldItem({
        value: '',
        required: true,
        disabled: false,
    });

    name: FieldItem = new FieldItem({
        value: '',
        required: true,
        disabled: false,
    });

    description: FieldItem = new FieldItem({
        value: '',
        disabled: false,
    });

}

export class TagSearchFields implements TagSearchBean<FieldItem> {
    appId: FieldItem = new FieldItem({
        value: -1,
        hasFeedback: true,
        required: true,
        disabled: false,
        option: [{
            id: -1,
            name: '请选择',
            title: '请选择',
            description: '请选择'
        }]
    });
    name: FieldItem = new FieldItem({
        value: ''
    });
}

const addDefaultFields = toPlainObjectDeep(new TagFields());
const editDefaultFields = toPlainObjectDeep(new TagFields('edit'));

export class TagBaseFormStore extends FormStore<TagFields, TagBean<any>> {
    @observable confirmLoading: boolean = false;

    @action
    changeConfirmLoading(params) {
        this.confirmLoading = params;
    }

    validators = {
        appId: [isRequired],
        code: [isRequired, isNotHan],
        name: [isRequired]
    };

    @action
    getAppId(partnerId = '') {
        return getAppService.then((data) => {
            this.fields.appId.option = _.cloneDeep(toJSDeep(this.fields.appId.option)).concat(data);
            return data;
        })
    }

    @action
    bindData(data: TagBean<any>) {
        this.getAppId();

    }
}

export class Add extends TagBaseFormStore {
    defaultFields = _.cloneDeep(addDefaultFields);

    @observable fields: TagFields = _.cloneDeep(addDefaultFields);

    @action
    load = () => {
        this.getAppId();
        this.show();
    }

    @action
    addTag = (params: TagBean<any>) => {
        return tagController.addTag(params);
    }
}

export class Edit extends TagBaseFormStore {
    defaultFields = _.cloneDeep(editDefaultFields);

    @observable fields: TagFields = _.cloneDeep(editDefaultFields);

    @action
    load = (id: number) => {
        this.getAppId();
        tagController.checkTag({id: id})
            .then((res) => {
                if (res.success) {
                    let data = res.result;
                    this.show(data);
                    this.changeFields({
                        appId: {
                            disabled: true,
                        },
                        code: {
                            disabled: true,
                        },
                        name: {
                            disabled: true,
                        },
                    })
                } else {
                    message.error(res.message, 3);
                }
            })
    }

    @action
    editTag(params: TagBean<any>) {
        return tagController.editTag(params);
    }
}

// class Check extends TagBaseFormStore {
//     @observable fields: TagFields = _.cloneDeep(addDefaultFields);
// }

export class List extends ListStore<TagBean<any>> {
    @action
    searchTag(params?: any) {
        this._SpiLoading = true;

        tagController.searchTag(params).then((data) => {
            this.searchCallback(data);
        })
    }

    @action
    delete(id: number) {
        return tagController.deleteTag({tagId: id});
    }

    @action
    load(params: any = {}) {
        this.searchTag(params);
    }

    @action
    refresh(search) {
        this.changeState(true);
        search.clearAll();
        search.getAppId();
        this.load(search.getValues());
    }
}

const searchDefaultFields = toPlainObjectDeep(new TagSearchFields());

class Search extends FormStore<TagSearchFields, TagSearchBean<any>> {

    defaultFields = _.cloneDeep(searchDefaultFields);

    @observable fields: TagSearchFields = _.cloneDeep(searchDefaultFields);

    @observable expand: boolean = false;

    @action
    getAppId(partnerId = '') {
        return getAppService.then((data) => {
            this.fields.appId.option = _.cloneDeep(toJSDeep(this.fields.appId.option)).concat(data);
            return data;
        })
    }

    @action
    OnExpand(expand) {
        this.expand = !expand;
    }

    @action
    clearSearch(list: List) {
        this.clearAll();
        this.getAppId();
        list.load(searchStore.getValues());
    }

    @action
    clearSearchState() {
        this.expand = false;
        this.clearAll();
        this.getAppId();
    }
}

const getAppFn = async (partnerId = '') => {
    const data = await tagController
        .getApps({query: partnerId});

    return Promise.resolve(data.result);
}

const getAppService = getAppFn();

export const listStore = new List();
export const addStore = new Add();
export const editStore = new Edit();
// export const checkStore = new Check();
export const searchStore = new Search();